$(function () {
    syb('#affiliationSyb');
    $('#myTable').dataTableExtend({
        "ajax": sysInfo.basePath + "/zs/credential/qualification/find_page_data.do",
//			    	"select":true,
        "scrollX": "true",//是否开启横项滚动
        "fixedColumns": {//锁定列，只有开启scrollX时才生效
            "leftColumns": 4,//左侧锁定2列
            // "rightColumns": 1//右侧锁定1列
        },
        "fnServerParams": function (aoData) {
            //搜索条件
            aoData.haveAuthority = checkHasSybAuth($('#affiliationSyb option'));
            aoData.signOrgId = $('[name="signOrgId"]').val();
            aoData.certificateType = $('[name="certificateType"]').val();
            aoData.certificateName = $('[name="certificateName"]').val();
            aoData.certificateLevel = $('[name="certificateLevel"]').val();
            aoData.overdueStatus = $('[name="overdueStatus"]').val();
            aoData.certificateStatus = $('[name="certificateStatus"]').val();
            aoData.certificateArea = $('[name="certificateArea"]').val();
            aoData.applicationScope = $('[name="applicationScope"]').val();
            aoData.conditions = $('[name="conditions"]').val();
            if ($("#systemDate").val()) {
                aoData.systemDate = $("#systemDate").val();
            }
            if ($("#certificateCode").val()) {
                aoData.certificateCode = $("#certificateCode").val();
            }
        },
        "aoColumns": [
            {
                "sClass": "check-th",
                "title": "<input type=\"checkbox\" class=\"checkall\" value=\"undefined\">",
                "orderable": false,
                "data": "ID",
                "render": function (data, type, full, meta) {
                    return '<input type="checkbox"  class="checkchild"  value="' + data + '" />';
                }
            },
            {
                "title": "序号",
                "render": function (data, type, full, meta) {
                    return meta.row + 1;
                },
                "orderable": false
            },
            {
                "title": "所属事业部",
                "data": "signOrgName",
                "orderable": false
            },
            {
                "title": "资质编号",
                "data": "certificateNo",
                "orderable": false
            },
            {
                "title": "资质所属企业",
                "data": "companyName",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return "<div class='f-toe' style=\"max-width:100px;word-wrap:break-word\">" + data + "</div>";
                }
            },
            {
                "title": "资质有效期",
                "data": "certificateExpiryTime",
                "orderable": false,
                "render": function (data) {
                    return data.substring(0, 10);
                }
            },
            {
                "title": "资质类型",
                "data": "certificateType",
                "orderable": false
            },
            {
                "title": "资质名称",
                "data": "certificateName",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return "<div title='" + data + "' class='f-toe' style=\"max-width:100px;word-wrap:break-word\">" + data + "</div>";
                }
            },
            {
                "title": "资质等级",
                "data": "certificateLevel",
                "orderable": false
            },
            {
                "title": "成本价格（元）",
                "data": "receiptPrice",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return fmtMoney(data);
                }
            },
            {
                "title": "资质状态",
                "data": "certificateStatus",
                "orderable": false
            },
            {
                "title": "超期情况",
                "data": "overdueStatus",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    return !checkIsEmpty(data) && data === "资质超期" ? "<span style='color:red'>资质超期</span><br/>" : "<span  style='color:green'>未超期</span><br/>";

                }
            },
            {
                "title": "地区",
                "data": "certificateArea",
                "orderable": false,
                "render": function (data, type, row, meta) {
                    areaProvince = (null != row.areaProvince && "" != row.areaProvince) ? row.areaProvince : "";
                    area = (null != row.certificateArea && "" != row.certificateArea) ? "/" + row.certificateArea : "";
                    return areaProvince + area;
                }
            },
            {
                "title": "适用范围",
                "data": "applicationScope",
                "orderable": false
            },
            {
                "title": "操作",
                "orderable": false,
                "data": "baseId",
                "render": function (data, type, full, meta) {
                    var reStr = '<a onclick="qualificationDetails(\'' + full.certificateNo + '\',\'' + data + '\')" class="iboss-link"  style="cursor:pointer">查看</a>';
                    reStr += '<a onclick="edit(\'' + data + '\',\'' + full.certificateStatus + '\')" class="iboss-link"  style="cursor:pointer">编辑</a>';
                    return reStr;
                }
            }
        ],
    });

    loadCertificateType('190px', '#certificateType', $("#generalType").val(), 0);

});

function edit(id, status) {
    var adminEdit = $("[name='adminEdit']").val();
    if ((status == "未使用" || checkIsEmpty(status)) || !checkIsEmpty(adminEdit)) {
        var title = "编辑资质信息";
        if (!id) {
            title = "新增资质信息";
        }
        DGG.loading();
        $.post(sysInfo.basePath + "/zs/credential/qualification/edit_page.html", {id: id}, function (data) {
            DGG.loading('close');
            dgg.Open({
                type: 1,
                title: title,
                content: data,
                area: '90%',
                yes: function (index) {
                    var params = $('#qualificationForm').serializeObject();
                    if (!$("#qualificationForm [name='certificateType']").val()) {
                        DGG.Warning("资质类型不能为空");
                        return false;
                    }
                    if (!$("#qualificationForm #certificateName").val()) {
                        DGG.Warning("资质名称不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateLevel']").val()) {
                        DGG.Warning("资质等级不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='applicationScope']").val()) {
                        DGG.Warning("适用范围不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='companyName']").val()) {
                        DGG.Warning("资质所属企业不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateNo']").val()) {
                        DGG.Warning("资质编号不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='receiptPrice']").val()) {
                        DGG.Warning("成本价格不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateExpiryTime']").val()) {
                        DGG.Warning("资质过期时间不能为空");
                        return;
                    }
                    if (!$("#qualificationForm [name='certificateArea']").val()) {
                        DGG.Warning("地区不能为空");
                        return;
                    }
                    var certificateName = "";
                    $.each($("#qualificationForm #certificateName").select2("val"), function (i, v) {
                        certificateName += v + ",";
                    });
                    if (checkEmpty(certificateName)) {
                        certificateName = certificateName.substring(0, certificateName.length - 1);
                        params.certificateName = certificateName;
                    }
                    if (certificateName.split(",").length > 5) {
                        DGG.Warning("最多只能选择五个资质名称！");
                        return;
                    }
                    // 新增证书
                    params.certificateSpecialty = getVal('#qualificationForm #certificateName', 0);
                    var index1 = DGG.Confire("是否保存数据？", "保存提示", function () {
                        DGG.loading();
                        $.post(sysInfo.basePath + "/zs/credential/qualification/save_edit.do", params, function (re) {
                            DGG.loading('close');
                            if (re.code == 0) {
                                layer.close(index);
                                reloadTable();
                                DGG.Succeed("保存成功！");
                            } else {
                                DGG.Errors(re.msg);
                            }
                        }, 'json');
                    });
                }
            });
        }, 'html');
    } else {

        layer.msg("该资质信息只能修改资质状态为未使用的！")
    }
}

//搜索
function reloadTable() {
    $('#myTable').dataTableExtend("reload");
}

//导出数据
function derivedTable() {
    var signOrgId = $('[name="signOrgId"]').val();
    var certificateType = $('[name="certificateType"]').val();
    var certificateName = $('[name="certificateName"]').val();
    var certificateLevel = $('[name="certificateLevel"]').val();
    var overdueStatus = $('[name="overdueStatus"]').val();
    var certificateStatus = $('[name="certificateStatus"]').val();
    var certificateArea = $('[name="certificateArea"]').val();
    var applicationScope = $('[name="applicationScope"]').val();
    var conditions = $('[name="conditions"]').val();
    if ((null != signOrgId && "" != signOrgId) || "" != certificateType || "" != certificateName || "" != certificateLevel || "" != overdueStatus || "" != certificateStatus || "" != certificateArea || "" != applicationScope || "" != conditions) {
        DGG.Confire('确定要导出资质信息？', '导出提示', function () {
            var form = $('<form method="POST" action="' + sysInfo.basePath + '/zs/credential/qualification/export_bag_data.do">');
            if (null != signOrgId && "" != signOrgId) {
                form.append($('<input type="hidden" name="signOrgId" value="' + signOrgId + '">'));
            }
            form.append($('<input type="hidden" name="certificateType" value="' + certificateType + '">'));
            form.append($('<input type="hidden" name="certificateName" value="' + certificateName + '">'));
            form.append($('<input type="hidden" name="certificateLevel" value="' + certificateLevel + '">'));
            form.append($('<input type="hidden" name="overdueStatus" value="' + overdueStatus + '">'));
            form.append($('<input type="hidden" name="certificateStatus" value="' + certificateStatus + '">'));
            form.append($('<input type="hidden" name="certificateArea" value="' + certificateArea + '">'));
            form.append($('<input type="hidden" name="applicationScope" value="' + applicationScope + '">'));
            form.append($('<input type="hidden" name="conditions" value="' + conditions + '">'));
            form.append($('<input type=\"hidden\" name=\"length\" value=\"500000\">'));
            form.appendTo('body').submit();
        });

    } else {
        layer.msg("至少有一个条件才能导出！");
        return;
    }


}

//钱的转换
function transform(money) {
    var nStr = money;
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x[1];
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1,$2');
    }
    if (!checkIsEmpty(x2)) {
        return x1 + '.' + (x2 ? x2.replace(/(\d{3})(?=[^$])/g, '$1,') : '');
    } else {
        return x1;
    }


}